package com.xwh.article.service.impl;

import cn.hutool.core.lang.tree.Tree;
import cn.hutool.core.lang.tree.TreeNodeConfig;
import cn.hutool.core.lang.tree.TreeUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xwh.article.entity.SoftwareType;
import com.xwh.article.mapper.SoftwareTypeMapper;
import com.xwh.article.service.SoftwareTypeService;
import com.xwh.core.exception.FailException;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author xwh
 **/
@Service
public class SoftwareTypeServiceImpl extends ServiceImpl<SoftwareTypeMapper, SoftwareType> implements SoftwareTypeService {

    @Override
    public List<Tree<String>> listTree() {
        List<SoftwareType> list = list();
        //配置
        TreeNodeConfig treeNodeConfig = new TreeNodeConfig();
        //关闭排序
        treeNodeConfig.setWeightKey("softwareTypeOrder");
        treeNodeConfig.setIdKey("softwareTypeId");
        // 自定义属性名 都要默认值的
        // 最大递归深度
        treeNodeConfig.setDeep(3);
        return TreeUtil.build(list, "0", treeNodeConfig,
                (treeNode, tree) -> {
                    tree.setId(String.valueOf(treeNode.getSoftwareTypeId()));
                    tree.setParentId(treeNode.getParentSoftwareTypeId());
                    tree.putExtra("softwareTypeName", treeNode.getSoftwareTypeName());
                    tree.putExtra("softwareTypeStatus", treeNode.getSoftwareTypeStatus());
                    tree.putExtra("softwareTypeDescription", treeNode.getSoftwareTypeDescription());
                });
    }

    @Override
    public void add(SoftwareType softwareType) {
        SoftwareType one = lambdaQuery().eq(SoftwareType::getSoftwareTypeName, softwareType.getSoftwareTypeName()).one();
        if (one != null) {
            throw new FailException("软件类型名称不能重复");
        }
        save(softwareType);
    }
}
